<!-- saved from url=(0014)about:internet -->
<pre class="code">
<span class="srcline"><span class="lineno"><a href="1,1" id="srcline1"> 1</a></span><span class="line"><span class="keyword">function</span> <span class="var type1" id="S2T29U3">dy</span> = master_function_v4(<span class="var type1" id="S3T3U6">t</span>, <span class="var type1" id="S4T29U7">y</span>, <span class="var type1" id="S5T1U8">x</span>, <span class="var type1" id="S6T7U9">transitions_matrix</span>, <span class="var type1" id="S7T3U10">force_rate</span>, <span class="var type1" id="S8T1U11">variables_indexes</span>,  <span class="var type1" id="S9T1U12">initial_rates</span>)</span></span>
<span class="srcline"><span class="lineno"><a href="1,2" id="srcline2"> 2</a></span><span class="line"></span></span>
<span class="srcline"><span class="lineno"><a href="1,3" id="srcline3"> 3</a></span><span class="line">    <span class="mxinfo" id="T3:U9"><span class="var type1" id="S10T3U15">s</span> = <span class="mxinfo" id="T3:U11">1</span></span>;</span></span>
<span class="srcline"><span class="lineno"><a href="1,4" id="srcline4"> 4</a></span><span class="line">    <span class="mxinfo" id="T3:U12"><span class="var type1" id="S11T3U19">N</span> = <span class="mxinfo" id="T3:U14"><span class="mxinfo" id="T3:U15">length(<span class="var type1" id="S9T1U23">initial_rates</span>)</span>/<span class="mxinfo" id="T3:U17">2</span></span></span>;</span></span>
<span class="srcline"><span class="lineno"><a href="1,5" id="srcline5"> 5</a></span><span class="line">    <span class="mxinfo" id="T7:U18"><span class="var type1" id="S13T7U27">rate_matrix</span> = <span class="var type1" id="S6T7U28">transitions_matrix</span></span>;</span></span>
<span class="srcline"><span class="lineno"><a href="1,6" id="srcline6"> 6</a></span><span class="line">    <span class="keyword">for</span> <span class="var type1" id="S14T3U31">i</span> = <span class="mxinfo" id="T3:U22">1</span>:<span class="mxinfo" id="T3:U23">length(<span class="var type1" id="S6T7U36">transitions_matrix</span>)</span></span></span>
<span class="srcline"><span class="lineno"><a href="1,7" id="srcline7"> 7</a></span><span class="line">        <span class="keyword">for</span> <span class="var type1" id="S15T3U39">j</span> = <span class="mxinfo" id="T3:U26">1</span>:<span class="mxinfo" id="T3:U27">length(<span class="var type1" id="S6T7U44">transitions_matrix</span>)</span></span></span>
<span class="srcline"><span class="lineno"><a href="1,8" id="srcline8"> 8</a></span><span class="line">            <span class="keyword">if</span>(<span class="mxinfo" id="T4:U29"><span class="mxinfo" id="T3:U30"><span class="var type1" id="S6T7U50">transitions_matrix</span>(<span class="var type1" id="S14T3U51">i</span>, <span class="var type1" id="S15T3U52">j</span>)</span> &gt; <span class="mxinfo" id="T3:U34">0</span></span>)</span></span>
<span class="srcline"><span class="lineno"><a href="1,9" id="srcline9"> 9</a></span><span class="line">                <span class="mxinfo" id="T1:U35"><span class="var type1" id="S16T1U56">index</span> = <span class="mxinfo" id="T1:U37">find(<span class="mxinfo" id="T40:U38"><span class="var type1" id="S8T1U60">variables_indexes</span>==<span class="var type1" id="S10T3U61">s</span></span>)</span></span>; </span></span>
<span class="srcline"><span class="lineno"><a href="1,10" id="srcline10">10</a></span><span class="line">                <span class="keyword">if</span>(<span class="mxinfo" id="T4:U41"><span class="mxinfo" id="T3:U42">sum(<span class="var type1" id="S16T1U68">index</span>)</span>&gt;<span class="mxinfo" id="T3:U44">0</span></span>)</span></span>
<span class="srcline"><span class="lineno"><a href="1,11" id="srcline11">11</a></span><span class="line">                    <span class="mxinfo" id="T1:U45"><span class="var type1" id="S19T1U72">k_0</span> = <span class="mxinfo" id="T1:U47"><span class="var type1" id="S5T1U74">x</span>(<span class="var type1" id="S16T1U75">index</span>)</span></span>;</span></span>
<span class="srcline"><span class="lineno"><a href="1,12" id="srcline12">12</a></span><span class="line">                <span class="keyword">else</span></span></span>
<span class="srcline"><span class="lineno"><a href="1,13" id="srcline13">13</a></span><span class="line">                    <span class="mxinfo" id="T1:U50"><span class="var type1" id="S19T1U79">k_0</span> = <span class="mxinfo" id="T3:U52"><span class="var type1" id="S9T1U81">initial_rates</span>(<span class="var type1" id="S10T3U82">s</span>)</span></span>;</span></span>
<span class="srcline"><span class="lineno"><a href="1,14" id="srcline14">14</a></span><span class="line">                <span class="keyword">end</span></span></span>
<span class="srcline"><span class="lineno"><a href="1,15" id="srcline15">15</a></span><span class="line">                </span></span>
<span class="srcline"><span class="lineno"><a href="1,16" id="srcline16">16</a></span><span class="line">                <span class="mxinfo" id="T1:U55"><span class="var type1" id="S16T1U85">index</span> = <span class="mxinfo" id="T1:U57">find(<span class="mxinfo" id="T40:U58"><span class="var type1" id="S8T1U89">variables_indexes</span>==(<span class="mxinfo" id="T3:U60"><span class="var type1" id="S10T3U92">s</span>+<span class="var type1" id="S11T3U93">N</span></span>)</span>)</span></span>; </span></span>
<span class="srcline"><span class="lineno"><a href="1,17" id="srcline17">17</a></span><span class="line">                <span class="keyword">if</span>(<span class="mxinfo" id="T4:U63"><span class="mxinfo" id="T3:U64">sum(<span class="var type1" id="S16T1U100">index</span>)</span>&gt;<span class="mxinfo" id="T3:U66">0</span></span>)</span></span>
<span class="srcline"><span class="lineno"><a href="1,18" id="srcline18">18</a></span><span class="line">                    <span class="mxinfo" id="T1:U67"><span class="var type1" id="S20T1U104">x_0</span> = <span class="mxinfo" id="T1:U69"><span class="var type1" id="S5T1U106">x</span>(<span class="var type1" id="S16T1U107">index</span>)</span></span>;</span></span>
<span class="srcline"><span class="lineno"><a href="1,19" id="srcline19">19</a></span><span class="line">                <span class="keyword">else</span></span></span>
<span class="srcline"><span class="lineno"><a href="1,20" id="srcline20">20</a></span><span class="line">                    <span class="mxinfo" id="T1:U72"><span class="var type1" id="S20T1U111">x_0</span> = <span class="mxinfo" id="T3:U74"><span class="var type1" id="S9T1U113">initial_rates</span>(<span class="mxinfo" id="T3:U76"><span class="var type1" id="S10T3U115">s</span>+<span class="var type1" id="S11T3U116">N</span></span>)</span></span>;</span></span>
<span class="srcline"><span class="lineno"><a href="1,21" id="srcline21">21</a></span><span class="line">                <span class="keyword">end</span></span></span>
<span class="srcline"><span class="lineno"><a href="1,22" id="srcline22">22</a></span><span class="line">                </span></span>
<span class="srcline"><span class="lineno"><a href="1,23" id="srcline23">23</a></span><span class="line">                <span class="comment">%x_0</span></span></span>
<span class="srcline"><span class="lineno"><a href="1,24" id="srcline24">24</a></span><span class="line">                <span class="mxinfo" id="T3:U79"><span class="mxinfo" id="T3:U80"><span class="var type1" id="S13T7U120">rate_matrix</span>(<span class="var type1" id="S14T3U121">i</span>, <span class="var type1" id="S15T3U122">j</span>)</span> = <span class="mxinfo" id="T3:U84"><span class="mxinfo" id="T3:U85"><span class="var type1" id="S19T1U125">k_0</span>(<span class="mxinfo" id="T3:U87">1</span>)</span> * <span class="mxinfo" id="T3:U88">exp(<span class="mxinfo" id="T3:U89"><span class="mxinfo" id="T3:U90"><span class="mxinfo" id="T3:U91"><span class="var type1" id="S3T3U132">t</span>*<span class="var type1" id="S7T3U133">force_rate</span></span>*<span class="mxinfo" id="T3:U94"><span class="var type1" id="S20T1U135">x_0</span>(<span class="mxinfo" id="T3:U96">1</span>)</span></span>/<span class="mxinfo" id="T3:U97">4.1</span></span>)</span></span></span>;</span></span>
<span class="srcline"><span class="lineno"><a href="1,25" id="srcline25">25</a></span><span class="line">                </span></span>
<span class="srcline"><span class="lineno"><a href="1,26" id="srcline26">26</a></span><span class="line">                <span class="keyword">if</span>(<span class="mxinfo" id="T4:U98"><span class="mxinfo" id="T3:U99"><span class="var type1" id="S13T7U143">rate_matrix</span>(<span class="var type1" id="S14T3U144">i</span>, <span class="var type1" id="S15T3U145">j</span>)</span> &gt;= <span class="mxinfo" id="T3:U103">5000</span></span>)</span></span>
<span class="srcline"><span class="lineno"><a href="1,27" id="srcline27">27</a></span><span class="line">                    <span class="mxinfo" id="T3:U104"><span class="mxinfo" id="T3:U105"><span class="var type1" id="S13T7U150">rate_matrix</span>(<span class="var type1" id="S14T3U151">i</span>, <span class="var type1" id="S15T3U152">j</span>)</span> = <span class="mxinfo" id="T3:U109">5000</span></span>;</span></span>
<span class="srcline"><span class="lineno"><a href="1,28" id="srcline28">28</a></span><span class="line">                <span class="keyword">end</span></span></span>
<span class="srcline"><span class="lineno"><a href="1,29" id="srcline29">29</a></span><span class="line">                <span class="mxinfo" id="T3:U110"><span class="var type1" id="S10T3U156">s</span> = <span class="mxinfo" id="T3:U112"><span class="var type1" id="S10T3U158">s</span> + <span class="mxinfo" id="T3:U114">1</span></span></span>;</span></span>
<span class="srcline"><span class="lineno"><a href="1,30" id="srcline30">30</a></span><span class="line">            <span class="keyword">end</span></span></span>
<span class="srcline"><span class="lineno"><a href="1,31" id="srcline31">31</a></span><span class="line">        <span class="keyword">end</span></span></span>
<span class="srcline"><span class="lineno"><a href="1,32" id="srcline32">32</a></span><span class="line">    <span class="keyword">end</span></span></span>
<span class="srcline"><span class="lineno"><a href="1,33" id="srcline33">33</a></span><span class="line">    </span></span>
<span class="srcline"><span class="lineno"><a href="1,34" id="srcline34">34</a></span><span class="line">    <span class="mxinfo" id="T38:U115"><span class="var type1" id="S22T38U162">diagonal</span> = <span class="mxinfo" id="T38:U117">- <span class="mxinfo" id="T38:U118">sum(<span class="var type1" id="S13T7U166">rate_matrix</span>)</span></span></span>;</span></span>
<span class="srcline"><span class="lineno"><a href="1,35" id="srcline35">35</a></span><span class="line">    </span></span>
<span class="srcline"><span class="lineno"><a href="1,36" id="srcline36">36</a></span><span class="line">    <span class="keyword">for</span> <span class="var type1" id="S14T3U169">i</span> = <span class="mxinfo" id="T3:U121">1</span>:<span class="mxinfo" id="T3:U122">length(<span class="var type1" id="S6T7U174">transitions_matrix</span>)</span></span></span>
<span class="srcline"><span class="lineno"><a href="1,37" id="srcline37">37</a></span><span class="line">        <span class="mxinfo" id="T3:U124"><span class="mxinfo" id="T3:U125"><span class="var type1" id="S13T7U178">rate_matrix</span>(<span class="var type1" id="S14T3U179">i</span>, <span class="var type1" id="S14T3U180">i</span>)</span> = <span class="mxinfo" id="T3:U129"><span class="var type1" id="S22T38U182">diagonal</span>(<span class="var type1" id="S14T3U183">i</span>)</span></span>;</span></span>
<span class="srcline"><span class="lineno"><a href="1,38" id="srcline38">38</a></span><span class="line">    <span class="keyword">end</span></span></span>
<span class="srcline"><span class="lineno"><a href="1,39" id="srcline39">39</a></span><span class="line">    </span></span>
<span class="srcline"><span class="lineno"><a href="1,40" id="srcline40">40</a></span><span class="line">    <span class="mxinfo" id="T29:U132"><span class="var type1" id="S2T29U186">dy</span> = <span class="mxinfo" id="T29:U134"><span class="var type1" id="S13T7U188">rate_matrix</span> * <span class="var type1" id="S4T29U189">y</span></span></span>;</span></span>
<span class="srcline"><span class="lineno"><a href="1,41" id="srcline41">41</a></span><span class="line"><span class="keyword">end</span></span></span>
<span class="srcline"><span class="lineno"><a href="1,42" id="srcline42">42</a></span><span class="line"></span></span>
</pre>
